

import cn.hutool.core.lang.UUID;
import com.xiaominfo.oss.sdk.OSSClient;
import com.xiaominfo.oss.sdk.OSSClientProperty;
import com.xiaominfo.oss.sdk.client.FileBytesResponse;
import com.xiaominfo.oss.sdk.common.OSSClientMessage;
import model.Gnote;
import net.sf.json.JSONArray;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import util.MyHttpUtil;
import util.StaticDateBaseConnUtil;
import util.TxtReader;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class ReadHtml {

    /**
     * 日志对象
     */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * 用户id
     */
    private final String userId = "xxxxxxx";
    /**
     * 数据库
     */
    private StaticDateBaseConnUtil staticDateBaseConnUtil = null;
    /**
     * 时间格式化
     * 2013/2/16 08:40
     */
    private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/M/d HH:mm");

    public static void main(String[] args) {
        ReadHtml readHtml = new ReadHtml();
        readHtml.run();
    }

    /**
     * 路径
     */
    private String ROOT_PATH = "/home/godkiller/Documents/gnoteReader/Data/";

    /**
     * 上传到oss oss系统教程后续有空出一个
     *
     * @param file
     * @return
     */
    public static OSSClientMessage<FileBytesResponse> updaldFileToOssBase64(File file) {
        OSSClientProperty ossClientProperty = new OSSClientProperty();
        ossClientProperty.setRemote("http://xxxxxxxx:18000");
        ossClientProperty.setAppid("oss30g3q2d58s");
        ossClientProperty.setAppsecret("76o23e34sk38");
        ossClientProperty.setProject("test");
        OSSClient ossClient = new OSSClient(ossClientProperty, "BASE64");
        OSSClientMessage<FileBytesResponse> ossClientMessage = ossClient.uploadFile(file);
        return ossClientMessage;
    }

    /**
     * 扫描文件夹
     *
     * @param dir
     */
    private List<File> scanDir(String dir, Boolean getDir) {
        File dirFile = new File(dir);
        List<File> jsonArray = new ArrayList<File>();
        if (dirFile.exists() && dirFile.isDirectory()) {
            File[] tempList = dirFile.listFiles();
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isFile() && tempList[i].getName().contains(".")) {
                    String lastName = tempList[i].getName().substring(tempList[i].getName().lastIndexOf("."), tempList[i].getName().length());
                    if (!getDir && ".html".equals(lastName.toLowerCase())) {
                        jsonArray.add(tempList[i]);
                    }
                }
                if (tempList[i].isDirectory()) {
                    //这里就不递归了，
                    if (getDir) {
                        jsonArray.add(tempList[i]);
                    }
                }
            }
        }
//        logger.info(jsonArray.toString());
        return jsonArray;
    }

    /**
     * 上传到oss对象存储空间
     *
     * @param file
     * @return
     */
    public static OSSClientMessage<FileBytesResponse> updaldFileToOss(File file) {
        OSSClientProperty ossClientProperty = new OSSClientProperty();
        ossClientProperty.setRemote("http://xxxxx:18000");
        ossClientProperty.setAppid("oss30g3q2d58s");
        ossClientProperty.setAppsecret("76o23e34sk38");
        ossClientProperty.setProject("gnote");
        OSSClient ossClient = new OSSClient(ossClientProperty, "POST");
        OSSClientMessage<FileBytesResponse> ossClientMessage = ossClient.uploadFile(file);
        return ossClientMessage;
    }

    /**
     * 获取元素文本
     *
     * @param document
     * @param cssQuery
     * @return
     */
    private String getFirst(Document document, String cssQuery) {
        return this.getFirstElement(document, cssQuery).text();
    }

    /**
     * 获取第一个元素
     *
     * @param document
     * @param cssQuery
     * @return
     */
    private Element getFirstElement(Document document, String cssQuery) {
        return document.select(cssQuery).first();
    }

    private Element getFirst(Elements elements) {
        if (null != elements && elements.size() > 0) {
            return elements.first();
        } else {
            return null;
        }
    }

    /**
     * 封装的get请求
     *
     * @param url 请求地址
     * @return
     */
    private Document httpGet(String url) {
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(1000).userAgent(MyHttpUtil.getUserAgent()).get();
        } catch (IOException e) {
//            e.printStackTrace();
            logger.info("请求[" + url + "]失败,正在第一次重试。");
        }
        return document;
    }

    /**
     * run
     */
    private void run() {
        staticDateBaseConnUtil = StaticDateBaseConnUtil.getInstance(
                "jdbc:mysql:/127.0.0.1:3306/jeesite?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC",
                "web", "web");
//        logger.info("start");
        List<File> dirs = scanDir(ROOT_PATH, true);
//        logger.info(dirs.toString());
        List<Gnote> gnoteList = new ArrayList<Gnote>();
        for (File dir : dirs) {
            List<File> files = scanDir(dir.getAbsolutePath(), true);
            logger.info(files.toString());
            for (File file : files) {
                List<File> files2 = scanDir(file.getAbsolutePath(), false);
                for (File file2 : files2) {
                    Gnote gnote = this.getNotes(file2);
                    if (null != gnote) {
                        gnoteList.add(gnote);
                    }
                }

            }
        }
        logger.info(gnoteList.toString());
        this.writeToDataBase(gnoteList);

    }

    /**
     * 获取笔记
     *
     * @param file
     * @return
     */
    private Gnote getNotes(File file) {
        Gnote gnote = new Gnote();
        String text = TxtReader.readTextFile(file);
        try {
            Document document = Jsoup.parse(text);
            if (document != null) {
                Elements el1 = document.select("#header .text_overflow");
                String title = el1.first().text();
                gnote.setTitle(title);
                String type = this.getFirstElement(document, "input[name='X-Folder-Name']").val();
                if (null == type || "".equals(type)) {
                    type = "allType";
                }
                gnote.setType(type);
                String time = this.getFirstElement(document, ".content .create_time").text();
                String text1 = "创建时间: ";
                String text2 = " 修改时间: ";
                String createTime = time.substring(text1.length(), time.indexOf(text2));
                String updateTime = time.substring(time.indexOf(text2) + text2.length(), time.length());
                gnote.setCreateTime(sdf1.parse(createTime));
                gnote.setUpdateTime(sdf1.parse(updateTime));
                Element contentElement = this.getFirstElement(document, ".content .note_content");
                gnote.setHtml(contentElement.html());
                gnote.setNoteContent(contentElement.text());
                gnote.setUserId(userId);
                //图片
                JSONArray imagesJsonArray = new JSONArray();
                Elements images = document.select(".content div img");
                if (null != images && images.size() > 0) {
                    for (Element image : images) {
                        String src = image.attr("src");
                        File imageFile = new File(file.getParent() + "/" + src);
                        if (imageFile.exists()) {
                            //图片存在了
                            String imageInfo = this.getImageInfo(imageFile);
                            imagesJsonArray.add(imageInfo);
                        }
                    }
                }
                if (imagesJsonArray.size() > 0) {
                    gnote.setImageBase64List(imagesJsonArray.toString());
                }
                gnote.setId(UUID.fastUUID().toString(true));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gnote;
    }

    /**
     * 图片文件返回图片小学
     *
     * @param imageFile
     * @return
     */
    private String getImageInfo(File imageFile) {
        //上传图片
        OSSClientMessage<FileBytesResponse> ossClientMessage = updaldFileToOss(imageFile);
        logger.info(ossClientMessage.toString());
        if ("8200".equals(ossClientMessage.getCode()) && null != ossClientMessage.getData()) {
            return ossClientMessage.getData().getUrl();
        } else {
            logger.warning(ossClientMessage.getMessage());
        }
        return null;
    }

    /**
     * 写入数据库
     *
     * @param gnoteList
     * @return
     */
    private int writeToDataBase(List<Gnote> gnoteList) {
        int index = 0;
        staticDateBaseConnUtil.edit("delete from g_note", true);
        for (Gnote gnote : gnoteList) {
            index += staticDateBaseConnUtil.edit("insert into g_note(id, title, type, create_time, update_time, note_content, html, image_base64_list, user_id)" +
                            "values(?,?,?,?,?,?,?,?,?)", true,
                    gnote.getId(), gnote.getTitle(), gnote.getType(), gnote.getCreateTime(), gnote.getUpdateTime(), gnote.getNoteContent(), gnote.getHtml(), gnote.getImageBase64List(), gnote.getUserId());
        }
        return index;
    }

}
